#ifndef ISCANNER_HPP
#define ISCANNER_HPP

#include <zbar.h>
#include <iostream>
#include <opencv2/opencv.hpp>

class IScanner : public zbar::ImageScanner
{
public:
    IScanner()
        : zbar::ImageScanner()
    {
        set_config(zbar::ZBAR_NONE, zbar::ZBAR_CFG_ENABLE, 0);
        set_config(zbar::ZBAR_NONE, zbar::ZBAR_CFG_X_DENSITY, 1);
        set_config(zbar::ZBAR_NONE, zbar::ZBAR_CFG_Y_DENSITY, 1);
        set_config(zbar::ZBAR_QRCODE, zbar::ZBAR_CFG_ENABLE, 1);
    }

    bool scan(const cv::Mat &mgs, std::vector<std::string> &data)
    {
        zbar::Image imageOne(mgs.cols, mgs.rows, "Y800", (uchar *)mgs.data, mgs.cols*mgs.rows);
        zbar::ImageScanner::scan(imageOne);

        data.clear();
        for(zbar::Image::SymbolIterator symbol = imageOne.symbol_begin();
            symbol != imageOne.symbol_end();
            ++symbol)
        {
            if(symbol->get_type_name().compare("QR-Code")==0)
            {
                data.push_back(symbol->get_data());
            }
        }
        return !data.empty();
    }

    static void test()
    {
        cv::VideoCapture c(0);

        IScanner ms;

        bool res=false;

        cv::Mat m;
        cv::Mat mg;

        std::vector<std::string> data;

        while(!res && c.read(m))
        {
            cv::cvtColor(m,mg,CV_BGR2GRAY);
            res=ms.scan(mg,data);
            cv::imshow("m",m);
            cv::waitKey(1);
        }

        std::cout<<data[0]<<"\n"<<std::flush;

        c.release();
        cv::waitKey(100000);
    }

};

#endif // ISCANNER_HPP

